package com.ffapp.demo.other;

import android.app.Activity;
import android.content.Context;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Gallery.LayoutParams;

import com.ffapp.demo.R;
import com.ffapp.demo.basis.BasisApp;
import com.flyco.dialog.widget.NormalDialog;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import my.LogUtil;
import photoview.PhotoView;
import photoview.PhotoViewAttacher;


/**
 * @作者 fourfaith
 * @备注 新闻详情图片查看activity发、
 */
public class PhotoScanDialog extends NormalDialog {

	public static void toImgScanDialog(Activity context,
                                       String imgurl) {
		PhotoScanDialog dialog = new PhotoScanDialog(context,imgurl,"");
		dialog.show();
	}

	public static void toImgScanDialog(Activity context,
                                       ArrayList<String> images, int position) {
		PhotoScanDialog dialog = new PhotoScanDialog(context,images,position);
		dialog.show();
	}


	@BindView(R.id.photo_viewpager)
    ViewPager mGallery;
	ArrayList<String> mListBean;
	PicGalleryAdapterNew mAdapter;
	int index = 0;
	Activity mContext;
	View mVTitle;
	boolean isTitleVisible = true;

	public PhotoScanDialog(Activity context, String url,
                           String title) {
		super(context);
		mContext = context;
		mListBean = new ArrayList<>();
		mListBean.add(url);
	}
	public PhotoScanDialog(Activity context, ArrayList<String> images,
                           int position) {
		super(context);
		mContext = context;
		mListBean = images;
		index = position;
	}

	@Override
	public View onCreateView() {
//        return super.onCreateView();
		View inflate = LayoutInflater.from(mContext).inflate(R.layout.photo_scan_dialog, null);
		ButterKnife.bind(this, inflate);

		widthScale(1.0f);
		return inflate;
	}

	@Override
	public void setUiBeforShow() {
//        setCharPieView(chart_pie);
//		mGallery = (ViewPager) findViewById(R.id.photo_viewpager);
		setListView();
		if(index != 0){
			mGallery.setCurrentItem(index);
		}
	}


	private void setListView() {
		 mAdapter = new PicGalleryAdapterNew(mContext, new OnTapListener() {
			@Override
            public void OnTap() {
				// TODO Auto-generated method stub
				changeTitleMode();
				LogUtil.i("OnTap---->");
				dismiss();
			}
		});

		 mGallery.setAdapter(mAdapter);
	}



	private void changeTitleMode() {
//		isTitleVisible = !isTitleVisible;
//		mVTitle.clearAnimation();
//		if (isTitleVisible) {
//			mVTitle.setVisibility(View.VISIBLE);
//			mVTitle.startAnimation(AnimationUtils.loadAnimation(mContext,
//					R.anim.down_in));
//
//		} else {
//			mVTitle.startAnimation(AnimationUtils.loadAnimation(mContext,
//					R.anim.up_out));
//			mVTitle.setVisibility(View.GONE);
//		}
	}

	public interface OnTapListener {
		public void OnTap();

	}

	public class PicGalleryAdapterNew extends PagerAdapter {

		private Context context;
		private OnTapListener mOnTouchListener;

		// private int images[] = { R.drawable.chrysanthemum, R.drawable.desert,
		// R.drawable.hydrangeas,R.drawable.penguins};

		public PicGalleryAdapterNew(Context context,
                                    OnTapListener mOnTouchListener) {
			this.context = context;
			this.mOnTouchListener = mOnTouchListener;
		}

		@Override
		public int getCount() {
			return mListBean == null ? 0 : mListBean.size();
		}

		@SuppressWarnings("deprecation")
		@Override
		public View instantiateItem(ViewGroup container, int position) {
			PhotoView photoView = new PhotoView(context);
			photoView.setOnViewTapListener(new PhotoViewAttacher.OnViewTapListener() {
				@Override
                public void onViewTap(View view, float x, float y) {
					// TODO Auto-generated method stub
					mOnTouchListener.OnTap();
				}
			});
			String imgUrl = getItem(position);
			LogUtil.i("imgUrl-->"+imgUrl);

			if (imgUrl.contains("http")) {
//				BasisApp.mBitmapHandler.loadBitmap(new DisplayView(photoView, imgUrl));
				BasisApp.loadImg(mContext,imgUrl,photoView,R.drawable.base_img_default_square);


			} else {
//				BasisApp.mBitmapHandler.loadBitmap(new DisplayView(photoView, imgUrl));
//				BasisApp.imageLoader.displayImage(Constants.FILE_PREFIX + imgUrl,
//						photoView, BasisApp.defautOptions,
//						BasisApp.defautDisplaylitener);
				BasisApp.loadImg(mContext,imgUrl,photoView,R.drawable.base_img_default_square);
			}
			photoView.setMinimumScale(0.5f);
			container.addView(photoView, LayoutParams.MATCH_PARENT,
					LayoutParams.MATCH_PARENT);
			return photoView;
		}

		private String getItem(int position) {
			if (position > -1 && position < getCount()) {
                return mListBean.get(position);
            }
			return "";
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView((View) object);
		}

		@Override
		public boolean isViewFromObject(View view, Object object) {
			// TODO Auto-generated method stub
			return view == object;
		}

	}

}
